package org.shangrila.app.wfm.mb;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.shangrila.app.sys.shiro.SecUtil;
import org.shangrila.wfm.core.WfmEngine;
import org.shangrila.wfm.core.WfmHelper;
import org.shangrila.wfm.eo.WfmProcess;
import org.shangrila.wfm.eo.WfmTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class WfmHomeMB implements Serializable{
	private static final Logger log = LoggerFactory.getLogger(WfmHomeMB.class);
	private static final long serialVersionUID = 4899456092581896951L;
	private String operator;  
	private String taskid="";  
	private String processid="";  
	private List voTasks=null ; 
	private List voProcesses=null ; 
	private List voServices=null ; 
	private WfmEngine engine = null;	
	private WfmTask selectedTask;
	private WfmProcess selectedProcess;
	private WfmProcess selectedService;

	public WfmHomeMB() {
		engine = WfmHelper.getEngine();
		try{
			operator = SecUtil.getCachedShiroUser().getUserno();
		}
		catch(Exception ex){
			operator="wfm";
		}

		voTasks = engine.getWfmdao().findTaskByOperatorOrRoles(operator, SecUtil.getCachedShiroRoles());
		voServices = engine.getWfmdao().findServiceByCreator(operator);
		voProcesses=engine.getWfmdao().findProcessAll();
		
	}

	//start process
    public String handleStartProcess() {
		engine.startService(selectedProcess.getId(), operator,null); //"operator"是流程实例创建者
		showMessages("Succesful", selectedProcess.getId() + " is started.");
    	return null;
    }
	
	//处理行选择
   public void onTodoRowSelect(SelectEvent event) {
	    selectedTask = (WfmTask) event.getObject();	   
   }
 
    public void onTodoRowUnselect(UnselectEvent event) {
    }

 	//show faces Messages
	public void showMessages(String summary,String detail) {	
		FacesMessage message = new FacesMessage(summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}



	public String getOperator() {
		return operator;
	}



	public void setOperator(String operator) {
		this.operator = operator;
	}



	public String getTaskid() {
		return taskid;
	}



	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}



	public String getProcessid() {
		return processid;
	}



	public void setProcessid(String processid) {
		this.processid = processid;
	}



	public List getVoTasks() {
		return voTasks;
	}



	public void setVoTasks(List voTasks) {
		this.voTasks = voTasks;
	}



	public List getVoProcesses() {
		return voProcesses;
	}



	public void setVoProcesses(List voPrcoesses) {
		this.voProcesses = voProcesses;
	}



	public WfmTask getSelectedTask() {
		return selectedTask;
	}



	public void setSelectedTask(WfmTask selectedTask) {
		this.selectedTask = selectedTask;
	}



	public WfmProcess getSelectedProcess() {
		return selectedProcess;
	}



	public void setSelectedProcess(WfmProcess selectedProcess) {
		this.selectedProcess = selectedProcess;
	}

	/**
	 * @return the voServices
	 */
	public List getVoServices() {
		return voServices;
	}

	/**
	 * @param voServices the voServices to set
	 */
	public void setVoServices(List voServices) {
		this.voServices = voServices;
	}

	/**
	 * @return the selectedService
	 */
	public WfmProcess getSelectedService() {
		return selectedService;
	}

	/**
	 * @param selectedService the selectedService to set
	 */
	public void setSelectedService(WfmProcess selectedService) {
		this.selectedService = selectedService;
	}

}
